package com.nju.edu.erp.service.Impl;

import com.nju.edu.erp.dao.SalarySheetDao;
import com.nju.edu.erp.enums.sheetState.ReceiptSheetState;
import com.nju.edu.erp.enums.sheetState.SalarySheetState;
import com.nju.edu.erp.model.po.AccountPO;
import com.nju.edu.erp.model.po.CustomerPO;
import com.nju.edu.erp.model.po.ReceiptSheetPO;
import com.nju.edu.erp.model.po.SalarySheetPO;
import com.nju.edu.erp.model.vo.UserVO;
import com.nju.edu.erp.model.vo.receipt.ReceiptSheetVO;
import com.nju.edu.erp.model.vo.salary.SalarySheetVO;
import com.nju.edu.erp.service.finance.AccountService;
import com.nju.edu.erp.service.finance.FinanceSalaryService;
import com.nju.edu.erp.service.finance.AccountService;
import com.nju.edu.erp.utils.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class  FinanceSalaryServiceImp implements FinanceSalaryService {
    SalarySheetDao salarySheetDao;
    AccountService accountService;
    @Autowired
    public FinanceSalaryServiceImp(SalarySheetDao salarySheetDao,AccountService accountService) {
        this.salarySheetDao = salarySheetDao;
        this.accountService=accountService;
    }

    @Override
    @Transactional
    public void makeSalarySheet(UserVO userVO, SalarySheetVO salarySheetVO) {
        SalarySheetPO salarySheetPO = new SalarySheetPO();
        BeanUtils.copyProperties(salarySheetVO, salarySheetPO);
        // 此处根据制定单据人员确定操作员
        salarySheetPO.setOperator(userVO.getName());
        SalarySheetPO latest = salarySheetDao.getLatest();
        String id = IdGenerator.generateSheetId(latest == null ? null : latest.getId(), "GZD");
        salarySheetPO.setId(id);
        salarySheetPO.setState(SalarySheetState.PENDING);
        salarySheetDao.save(salarySheetPO);
    }

    @Override
    public List<SalarySheetVO> getSalarySheetByState(SalarySheetState state) {
        List<SalarySheetVO> res = new ArrayList<>();
        List<SalarySheetPO> all;
        if (state == null) {
            all = salarySheetDao.findAll();
        } else {
            all = salarySheetDao.findAllByState(state);
        }
        for (SalarySheetPO po : all) {
            SalarySheetVO vo = new SalarySheetVO();
            BeanUtils.copyProperties(po, vo);
            res.add(vo);
        }
        return res;
    }

    @Override
    @Transactional
    public void approval(String salarySheetId, SalarySheetState state) {
            // TODO 审批完成, 修改一系列状态
            // 更新客户表(更新应付字段)
            // 更新应付 payable
            salarySheetDao.updateState(salarySheetId,state);
            SalarySheetPO salarySheet = salarySheetDao.findOneById(salarySheetId);
            AccountPO accountPO=accountService.findAccountByName(salarySheet.getAccount());
            accountPO.setMoney(accountPO.getMoney().add(salarySheet.getSalaryreal()));

    }
}
